package com.yihu.cms.action;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.yihu.article.beans.ArticleCategory;
import com.yihu.article.beans.ArticleCategoryQuery;
import com.yihu.cms.service.ArticleCategoryService;
import com.yihu.cms.util.CommonUtils;

@Controller
public class ArticleCategoryController extends BaseController{
	
	ArticleCategoryService as = new ArticleCategoryService();
	final Logger logger = LoggerFactory.getLogger(ArticleCategoryController.class);
	//56a08f13b401e8167ddf2628
	@RequestMapping(value = "/articleCategory", method = RequestMethod.POST)
	public @ResponseBody Object addCategory(@RequestBody ArticleCategory category) {
		int result = as.add(JSON.toJSONString(category));
		return CommonUtils.getResult(result, null);
	}
	
	@RequestMapping(value = "/articleCategory/{id}", method = RequestMethod.PUT)
	public @ResponseBody Object updateCategory(@RequestBody ArticleCategory category,@PathVariable("id") String id) {
		logger.debug("id: "+id);
		category.setId(id);
		String json = JSON.toJSONString(category);
		logger.debug("json: "+json);
		int result = as.update(json);
		return CommonUtils.getResult(result, null);
	}
	
	@RequestMapping(value = "/articleCategory/{id}", method = RequestMethod.DELETE)
	public @ResponseBody Object deleteArticleCategory(@PathVariable("id") String id) {
		// logger.info("删除人员信息id=" + id);
		int result = as.delete(id);
		return CommonUtils.getResult(result, null);
	}
	
	@RequestMapping(value = "/articleCategory/{id}", method = RequestMethod.GET)
	public @ResponseBody Object getArticleCategory(@PathVariable("id") String id) {
		String categoryJson = as.getArticleCategoryById(id);
		return JSON.parseObject(categoryJson, ArticleCategory.class);
	}
	
	@RequestMapping(value = "/articleCategory/list", method = RequestMethod.POST)
	public @ResponseBody Object getArticleCategoryPaging(@RequestBody ArticleCategoryQuery query) {

		String json = JSON.toJSONString(query);
		String resultJson= as.getArticleCategorys(json);
		return JSON.parse(resultJson);
	}
}
